
bills = []

def record_income():
    """记录收入功能"""
    print("\n===== 记录收入 =====")
    date = input("请输入日期（YYYY-MM-DD）：")
    amount = input("请输入收入金额（正数）：")
    category = input("请输入收入类别（如工资、奖金）：")
    note = input("请输入备注（可选）：")
    
    
    income = {
        "type": "income", 
        "date": date,
        "amount": amount,
        "category": category,
        "note": note
    }
    bills.append(income)
    print(f"\n 收入记录成功！\n记录信息：{date} | 收入 {amount} 元 | 类别：{category}")

def record_expense():
    """记录支出功能"""
    print("\n===== 记录支出 =====")
    date = input("请输入日期（YYYY-MM-DD）：")
    amount = input("请输入支出金额（正数）：")
    category = input("请输入支出类别：")
    note = input("请输入备注（可选）：")
    
    
    expense = {
        "type": "expense", 
        "date": date,
        "amount": amount,
        "category": category,
        "note": note
    }
    bills.append(expense)
    print(f"\n 支出记录成功！\n记录信息：{date} | 支出 {amount} 元 | 类别：{category}")

def view_all_bills():
    print("\n===== 所有账单 =====")
    incomes = [b for b in bills if b["type"] == "income"]
    expenses = [b for b in bills if b["type"] == "expense"]
    
    print("【收入记录】")
    for bill in incomes:
        print(f"日期：{bill['date']} | 金额：{bill['amount']}元 | 类别：{bill['category']} | 备注：{bill['note']}")
    print("【支出记录】")
    for bill in expenses:
        print(f"日期：{bill['date']} | 金额：{bill['amount']}元 | 类别：{bill['category']} | 备注：{bill['note']}")
    if not incomes and not expenses:
        print("暂无账单记录")

def query_bills():
    print("\n===== 查询账单 =====")
    category = input("请输入要查询的类别（如工资、餐饮）：")
    matched_incomes = [b for b in bills if b["type"] == "income" and b["category"] == category]
    matched_expenses = [b for b in bills if b["type"] == "expense" and b["category"] == category]
    
    print(f"【{category}类-收入】")
    for bill in matched_incomes:
        print(f"日期：{bill['date']} | 金额：{bill['amount']}元 | 备注：{bill['note']}")
    print(f"【{category}类-支出】")
    for bill in matched_expenses:
        print(f"日期：{bill['date']} | 金额：{bill['amount']}元 | 备注：{bill['note']}")
    if not matched_incomes and not matched_expenses:
        print(f"暂无{category}类账单")

def budget_management():
    print("\n===== 预算管理 =====")
    budget = float(input("请设置本月预算（元）："))
    total_expense = sum(float(b["amount"]) for b in bills if b["type"] == "expense")
    surplus = budget - total_expense
    print(f"本月预算：{budget}元 | 总支出：{total_expense}元 | {'超支' if surplus < 0 else '剩余'}：{abs(surplus)}元")

def monthly_statistics():
    print("\n===== 月度统计 =====")
    month = input("请输入月份（如 05 表示5月）：")
    matched_incomes = [b for b in bills if b["date"].endswith(month) and b["type"] == "income"]
    matched_expenses = [b for b in bills if b["date"].endswith(month) and b["type"] == "expense"]
    print(f"【{month}月收入】：{sum(float(b['amount']) for b in matched_incomes)}元")
    print(f"【{month}月支出】：{sum(float(b['amount']) for b in matched_expenses)}元")

def main_menu():
    while True:
        print("\n" + "="*30)
        print("欢迎使用个人账单管理系统")
        print("="*30)
        print("请选择操作：")
        print("1. 记录收入")
        print("2. 记录支出")
        print("3. 查看所有账单")  
        print("4. 查询账单")      
        print("5. 预算管理")   
        print("6. 月度统计")    
        print("7. 退出系统")   
        print("="*30)
        choice = input("请输入选项序号（1-7）：")
        if choice == "1":
            record_income()
        elif choice == "2":
            record_expense()
        elif choice == "3": 
            view_all_bills()
        elif choice == "4":  
            query_bills()
        elif choice == "5":   
            budget_management()
        elif choice == "6":    
            monthly_statistics()
        elif choice == "7":    
            print("\n 感谢使用，再见！")
            break
        else:
            print("\n 输入错误！请重新输入 1-7")
#更改内容
if __name__ == "__main__":
    main_menu()
